/* 
 * File:   SymbolTableRecord.cpp
 * Author: Kamil Chaber
 * 
 */

#include "symboltablerecord.h"

SymbolTableRecord::SymbolTableRecord(string lexem) {
    this->lexem = lexem;
    this->type = RECORD_TYPE_NOT_SET;
    this->address = "";
}

SymbolTableRecord::SymbolTableRecord(string lexem, SymbolTableElementType type) {
    this->lexem = lexem;
    this->type = type;
    this->address = "";
}

bool SymbolTableRecord::isIntType() {
    return this->type == RECORD_TYPE_INT_NUMBER || 
            ((this->type == RECORD_TYPE_VARIABLE || this->type == RECORD_TYPE_TEMP_VARIABLE) && this->varValueType == VAR_VALUE_TYPE_INTEGER);
}

bool SymbolTableRecord::isRealType() {
    return this->type == RECORD_TYPE_REAL_NUMBER || 
            ((this->type == RECORD_TYPE_VARIABLE || this->type == RECORD_TYPE_TEMP_VARIABLE) && this->varValueType == VAR_VALUE_TYPE_REAL);
}

int SymbolTableRecord::getOrderNumOfTempVariable() {
    if (this->type == RECORD_TYPE_TEMP_VARIABLE) {
        //TEMP_VARIABLE_PREFIX+orderNum
        string orderNumString = this->lexem.substr(TEMP_VARIABLE_PREFIX.length(), this->lexem.length());
        return atoi(orderNumString.c_str());
    }
    
    return EMPTY_INT_VALUE;
}

string SymbolTableRecord::toString() {
    stringstream strBuilder;
    strBuilder << "LEXEM: '" << this->lexem << "'\t";
    strBuilder << "TYPE: '" << SymbolTableElementTypeStrings[this->type] << "'\t";
    strBuilder << "ADDRESS: '" << this->address << "'";
    if (this->type == RECORD_TYPE_VARIABLE || this->type == RECORD_TYPE_TEMP_VARIABLE) {
        strBuilder << "\tVALUE_TYPE: '" << VariableValueTypeStrings[this->varValueType] << "'";
    }

    string result = strBuilder.str();
    return result;
}

bool SymbolTableRecord::operator==(const SymbolTableRecord& other) {
    return (this->lexem == other.lexem);
}

SymbolTableRecord::~SymbolTableRecord() {
}

